import { defineStore } from 'pinia'

export const useTagsViewStore = defineStore('tagsView', {
  state: () => ({
    visitedViews: [],
    cachedViews: []
  }),
  
  actions: {
    // 添加访问视图
    addView(view) {
      this.addVisitedView(view)
      this.addCachedView(view)
    },
    
    // 添加已访问视图
    addVisitedView(view) {
      // 使用fullPath作为唯一标识，以便处理带参数的路由
      if (this.visitedViews.some(v => v.fullPath === view.fullPath)) return
      
      this.visitedViews.push(
        Object.assign({}, view, {
          title: view.meta.title || 'no-name'
        })
      )
    },
    
    // 添加缓存视图
    addCachedView(view) {
      if (this.cachedViews.includes(view.name)) return
      if (!view.meta.noCache) {
        this.cachedViews.push(view.name)
      }
    },
    
    // 删除视图
    delView(view) {
      return new Promise(resolve => {
        this.delVisitedView(view)
        this.delCachedView(view)
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        })
      })
    },
    
    // 删除已访问视图
    delVisitedView(view) {
      // 首先检查是否是首页标签，如果是则不关闭
      if (view.path === '/dashboard') {
        console.log('首页标签不能关闭')
        return
      }
      
      // 查找要删除的标签索引
      const index = this.visitedViews.findIndex(v => {
        return v.fullPath === view.fullPath || v.path === view.path
      })
      
      // 如果找到了标签，则删除它
      if (index !== -1) {
        this.visitedViews.splice(index, 1)
        console.log('成功删除标签:', view.path, '剩余标签:', this.visitedViews.length)
      } else {
        console.log('未找到要删除的标签:', view.path)
      }
    },
    
    // 删除缓存视图
    delCachedView(view) {
      const index = this.cachedViews.indexOf(view.name)
      if (index > -1) {
        this.cachedViews.splice(index, 1)
      }
    },
    
    // 删除其他视图
    delOthersViews(view) {
      return new Promise(resolve => {
        this.delOthersVisitedViews(view)
        this.delOthersCachedViews(view)
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        })
      })
    },
    
    // 删除其他已访问视图
    delOthersVisitedViews(view) {
      const filteredViews = this.visitedViews.filter(v => {
        return v.meta?.affix || v.path === view.path
      })
      this.visitedViews.splice(0, this.visitedViews.length, ...filteredViews)
      console.log('Store删除其他视图后，剩余标签:', this.visitedViews.length)
    },
    
    // 删除其他缓存视图
    delOthersCachedViews(view) {
      const index = this.cachedViews.indexOf(view.name)
      if (index > -1) {
        this.cachedViews = this.cachedViews.slice(index, index + 1)
      } else {
        this.cachedViews = []
      }
    },
    
    // 删除所有视图
    delAllViews() {
      return new Promise(resolve => {
        this.delAllVisitedViews()
        this.delAllCachedViews()
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        })
      })
    },
    
    // 删除所有已访问视图
    delAllVisitedViews() {
      // 保留固定的标签和首页标签
      const filteredViews = this.visitedViews.filter(tag => tag.meta?.affix || tag.path === '/dashboard')
      this.visitedViews.splice(0, this.visitedViews.length, ...filteredViews)
      console.log('Store删除所有视图后，剩余标签:', this.visitedViews.length)
    },
    
    // 删除所有缓存视图
    delAllCachedViews() {
      this.cachedViews = []
    },
    
    // 更新已访问视图
    updateVisitedView(view) {
      for (let v of this.visitedViews) {
        if (v.path === view.path) {
          v = Object.assign(v, view)
          break
        }
      }
    }
  }
})